package edu.fit5046.mymonashmate.activity;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import edu.fit5046.mymonashmate.R;
import edu.fit5046.mymonashmate.utils.HttpUtils;
import edu.fit5046.mymonashmate.utils.SimpleInfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ChangePWActivity extends Activity implements OnClickListener {
	public static final String url = "/entity.pin";
	EditText ps0;
	EditText ps1;
	EditText ps2;
	Button next;
	Button cancel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_changepw);
		ps0 = (EditText) findViewById(R.id.ps0);
		ps1 = (EditText) findViewById(R.id.ps1);
		ps2 = (EditText) findViewById(R.id.ps2);
		Button cancel = (Button) findViewById(R.id.button_changepw_cancel);
		Button next = (Button) findViewById(R.id.button_changepw_next);
		cancel.setOnClickListener(this);
		next.setOnClickListener(this);
	}

	@SuppressLint("ShowToast")
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.button_changepw_cancel:
			finish();
			break;
		case R.id.button_changepw_next:
			String p0 = ps0.getText().toString();
			String p1 = ps1.getText().toString();
			String p2 = ps2.getText().toString();
			if (p0.length() < 2 || p1.length() < 2 || p2.length() < 2) {
				Toast.makeText(this, "Pin Too short", Toast.LENGTH_LONG).show();
			} else {
				if (!p1.equals(p2)) {
					Toast.makeText(this, "Pin Not same", Toast.LENGTH_LONG)
							.show();
				} else {
					try {
						if (updatePin(p0, p1) > 0)
							finish();
					} catch (InterruptedException | ExecutionException
							| JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

			break;
		default:
			break;
		}
	}

	private int updatePin(String p0, String p1) throws InterruptedException,
			ExecutionException, JSONException {
		String result = new MyTask().execute(
				url + "/CheckAccPin/" + Integer.toString(SimpleInfo.localId)
						+ "/" + p0).get();
		if (result.contains("success")) {
			JSONObject js = new JSONObject();
			js.put("stuId", SimpleInfo.localId);
			js.put("pin", p1);
			String postResult = new MyTask().execute(url + "/edit",
					js.toString()).get();
			System.out.println(js);
			Toast.makeText(this, postResult, Toast.LENGTH_LONG).show();
			return 1;
		} else {
			Toast.makeText(this, "Wrong Pin", Toast.LENGTH_LONG).show();
			return -1;
		}

	}

	private class MyTask extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... prm) {
			// TODO Auto-generated method stub
			if (prm.length == 1)
				return HttpUtils.Get(prm[0]);
			if (prm.length == 2)
				return HttpUtils.Edit(prm[0], prm[1]);
			else
				return null;
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}
}
